package com.doform.android.xianggucheng.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.doform.android.xianggucheng.holder.ViewHolder;

import java.util.List;

/**
 * Created by Administrator on 2017/5/12.
 */

public abstract class CommonListAdapter<T> extends BaseAdapter {

    protected LayoutInflater mLayoutInflater;
    protected List<T> mDatas;
    protected Context mContext;
    protected final int mItemLayoutId;

    public CommonListAdapter(Context context, List<T> datas, int layoutId) {
        mLayoutInflater = LayoutInflater.from(context);
        this.mContext = context;
        this.mDatas = datas;
        this.mItemLayoutId = layoutId;
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public T getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder vh = getViewHolder(position, convertView, parent);

        convert(vh, mDatas,position);
        return vh.getConvertView();
    }

    protected abstract void convert(ViewHolder vh, List<T> datas, int position);

    private ViewHolder getViewHolder(int position, View convertView, ViewGroup parent) {
        return ViewHolder.get(mContext, convertView, parent, mItemLayoutId, position);
    }

}
